package com.vimae.android.toneranger;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.util.Log;

class PlayScript extends TimerTask {
	private ArrayList script;
	private int counter;
	private int masterCount;
	private int pauser = 0;
	ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_VOICE_CALL, ToneGenerator.MAX_VOLUME);
		
	@Override
	public void run() {
		//Log.w("ToneRanger", "Script started playing : '" + script.get(counter).toString() + "'");
		switch (pauser) {
		
		case 0:
			pauser++;
			char playMe = char.class.cast(script.get(counter));
			if(playMe == '.')
			{
				pauser = 50;
				break;
			}
			else
			{
			tg.startTone(Translator.translate(char.class.cast(script.get(counter))));
			break;
			}
			
		case 2:
			tg.stopTone();
			masterCount++;
			pauser++;
			if(masterCount == counter + 1)
			{
				this.cancel();
			}
			break;
			
		case 4:
			counter++;
			pauser = 0;
			break;
			
		case 60:
			pauser = 3;
			break;
			
		default:
			pauser++;
			break;
		}
	}
	
	public PlayScript (String script) {
		Log.d("ToneRanger", "Setting script variable of PlayScript");
		char[] listing = script.toLowerCase().toCharArray();
		this.script = new ArrayList();
		for(char c : listing)
		{
			this.script.add(c);
		}
		masterCount = script.length();
	}

}